package org.meetunexpectedly.controller;

import org.meetunexpectedly.entity.video.Comment;
import org.meetunexpectedly.entity.vo.BasePage;
import org.meetunexpectedly.entity.vo.Result;
import org.meetunexpectedly.holder.UserHolder;
import org.meetunexpectedly.service.video.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @Description: 评论控制类
 * @Author: pupil
 * @Date: 2025/1/19 下午 9:40
 */
@RestController
@RequestMapping("/meetunexpectedly/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 评论/回复评论
     *
     * @param videoId  视频id
     * @param parentId 父级评论id
     * @param content  评论内容
     * @return
     */
    @PostMapping
    public Result addComment(@RequestParam Long videoId, @RequestParam Long parentId, @RequestParam String content) {
        if (!commentService.addComment(videoId, parentId, content, UserHolder.get())){
            return Result.error().message("发布评论失败");
        }
        return Result.ok().message("发布评论成功");
    }

    /**
     * 查询视频下的评论
     *
     * @param basePage 分页
     * @param videoId  视频id
     * @return
     */
    @GetMapping("/by/video/{videoId}")
    public Result getComment(BasePage basePage, @PathVariable Long videoId) {
        Collection<Comment> commentList = commentService.getCommentByVideoId(basePage, videoId);
        return Result.ok().data(commentList);
    }

    /**
     * 查询根评论下的子评论
     *
     * @param basePage 分页
     * @param rootId   根评论id
     * @return
     */
    @GetMapping("/by/rootComment")
    public Result getChildComment(BasePage basePage, @RequestParam Long rootId) {
        Collection<Comment> commentList = commentService.getCommentByRootId(basePage, rootId);
        return Result.ok().data(commentList);
    }

    /**
     * 删除评论
     *
     * @param commentId 评论id
     * @return
     */
    @DeleteMapping("/{commentId}")
    public Result deleteComment(@PathVariable Long commentId) {
        if (!commentService.deleteCommentById(commentId, UserHolder.get())) {
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }

    /**
     * 点赞/取消点赞
     *
     * @param commentId 评论id
     * @return
     */
    @PostMapping("/like/{commentId}")
    public Result thumbUpComment(@PathVariable Long commentId) {
        String msg = "已点赞";
        if (!commentService.thumbUpComment(commentId)) {
            msg = "取消点赞";
        }
        return Result.ok().message(msg);
    }
}
